// pages/common/components/xpProductCard/xpProductCard.js
import {
  live_del_g
} from '../../../../utils/imapi.js';
var app = getApp();
Component({
    properties: {
      productList: {
        type:Array,
        value:[],
        observer:function(newVal, oldVal){
          if(!oldVal[0])return;
          for(var i = 0;i<oldVal.length;i++){
            if(newVal[i]){
              if(newVal[i].item_id==oldVal[i].item_id){
                newVal[i] = oldVal[i]
              }
            }
          }
          this.setData({
            productList:newVal
          })
		  
        }
      },
	  selectList:{
	  		 type:Array,
	  		 value:[],
	  		 observer:function(newVal, oldVal){
	  			this.setData({
	  			  dotList:newVal
	  			})
				
				for(var i=0;i<this.data.dotList.length;i++){
					var index=this.data.productList.findIndex(item=>{
						return item.goods_title==this.data.dotList[i].goods_title
					})
					//console.log(index)
					this.data.productList[index].is_select = true;
				}
	  		},
			 
	  },
      showTitle:Boolean,
      bgColor:String,
      is_select:{
          type: [Boolean, String],
          default: false
      },
      is_confirm:{
        type: Boolean,
        value: false
      },
      is_add:{
        type: Boolean,
        value: true
      },
    },
	onLoad:function(){
		//console.log(this.data.selectList)
		// this.setData({
		// 	dotList:this.properties.productList
		// })
		// console.log(this.data.dotList)
	},
    data: {
        productList: [],
		dotList:[]
    },
    ready() {
		// this.setData({
		//    dotList:this.data.selectList
		// });
		// console.log(this.data.dotList)
		// //console.log(this.data.productList)
		// for(var i=0;i<this.data.dotList.length;i++){
		// 	var index=this.data.productList.findIndex(item=>{
		// 		return item.goods_title==this.data.dotList[i].title
		// 	})
		// 	//console.log(index)
		// 	this.data.productList[index].is_select = true;
		// }
    //   this.setData({
    //      dotList:this.properties.productList
    //    });
	   // console.log(this.data.dotList)
    },
    methods: {
		getList(){
			 //console.log(this.data.dotList)
			 //this.data.productList[0].is_select = true;

		},
		goods_delete(e){
			console.log(e);
			var that=this;
			var goods_code=e.currentTarget.dataset.goods_code;
			wx.showModal({			
			     title: '提示',			
			     content: '确定要删除商品？',		
			     success:async function (res) {		
			       if (res.confirm) {		
			         const goods=await live_del_g({goods_code:goods_code});
			         console.log(goods);
					 if(goods.code==0){
						 that.triggerEvent('onLoad');
					 }
			       } else {//这里是点击了取消以后			
			         console.log('用户点击取消')		
			       }		
			     }		
			   })
			
		
			
    },
    
    item_detail_handler(e){


    //  let p = e.currentTarget.dataset.p;
      app.globalData.cur_product={}
      let weapp_path = e.currentTarget.dataset.weapp_path;


      wx.navigateTo({
        url: '/'+weapp_path,
      })

    },
        //商品详情
        productDetail(e) {
            var id = e.currentTarget.dataset.id;
            var p = e.currentTarget.dataset.p;
            var index = e.currentTarget.dataset.index;
            app.globalData.cur_product = this.data.productList[index];
            wx.navigateTo({
                url: '/pages/module/productDetail/productDetail?id=' + id + '&p=' + p,
            })
        },
        chooseProduct(e){
            var index = e.currentTarget.dataset.index;
            this.triggerEvent('change', {index: e.currentTarget.dataset.index});
        },
        addProduct(e){  //  商品 添加到直播记录事件
		console.log("添加商品");
		//console.log(e);
		//console.log(this.data.productList);
          var that = this;
          var index = e.currentTarget.dataset.index;
          that.imgInfo = this.data.productList[index];
          const query = wx.createSelectorQuery().in(this);
          query.select(`#image${index}`).boundingClientRect();
          query.exec((res) => {
            that.imgInfo.top = res[0].top + 20;
            that.imgInfo.left = res[0].left + 20;
            that.imgInfo.scale = 2;
            that.imgInfo.show = true;
            this.data.productList[index].is_select = true;
            that.triggerEvent('add', {index: index,imgInfo: that.imgInfo});
          });
        },
        change(e){ // 商品移除
		console.log("移除商品");
		//console.log(e);
		//console.log(this.data.productList);
          var that = this;
          var index = e.currentTarget.dataset.index;
          that.imgInfo = this.data.productList[index];
          const query = wx.createSelectorQuery().in(this);
          query.select(`#image${index}`).boundingClientRect();
          query.exec((res) => {
            that.imgInfo.top = res[0].top + 20;
            that.imgInfo.left = res[0].left + 20;
            that.imgInfo.scale = 2;
            that.imgInfo.show = true;
            this.data.productList[index].is_select = false;
            that.triggerEvent('add', {index: index,imgInfo: that.imgInfo});
          });
        },
        imgLoad(e){
          var index = e.currentTarget.dataset.index;
          this.data.productList[index].imgShow=true;
          this.setData({
            productList:this.data.productList
          })
        }
    },
})